package com.mtnz.njk.Controller;

import com.mtnz.njk.Service.UserManagerService;
import com.mtnz.njk.entity.ApplyVipEntity;
import com.mtnz.njk.entity.NjkUserCropEntity;
import com.mtnz.njk.entity.NjkUserEntity;
import com.mtnz.njk.result.ResponseMessage;
import com.mtnz.njk.result.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

/**
 * @Description: app后台管理用户管理控制层
 * @Author: FBL
 * @CreateDate: 2019\8\19 0019 10:06
 * @Version: 1.0
 */
@RestController
@RequestMapping("/user")
@CrossOrigin
public class UserManagerController {
    @Autowired
    UserManagerService userManagerService;

    /**
     * 用户列表
     *
     * @param key
     * @param pageNum
     * @param pageSize
     * @return
     * @throws Exception
     */
    @PostMapping("/listUserInfo")
    public ResponseResult listUserInfo(String serviceId, Integer isConnection, Integer isVIP, String key, @RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize) throws Exception {
        return userManagerService.listUserInfo(serviceId, isConnection, isVIP, key, pageNum, pageSize);
    }

    /**
     * 修改用户信息
     *
     * @param userEntity
     * @return
     * @throws Exception
     */
    @PostMapping("/updateUserInfo")
    public ResponseResult updateUserInfo(@RequestBody NjkUserEntity userEntity) throws Exception {
        if (userEntity == null)
            return new ResponseResult(ResponseMessage.NULL_REQUEST);
        return userManagerService.updateUserInfo(userEntity);
    }

    /**
     * 修改用户作物以及与之关联的产品信息
     *
     * @param userCropEntity
     * @return
     * @throws Exception
     */
    @PostMapping("/updateUserCrop")
    public ResponseResult updateUserCrop(@RequestBody NjkUserCropEntity userCropEntity) throws Exception {
        if (userCropEntity == null)
            return new ResponseResult(ResponseMessage.NULL_REQUEST);
        return userManagerService.updateUserCrop(userCropEntity);
    }

    /**
     * 新增用户作物以及相关产品
     *
     * @param userCropEntity
     * @return
     * @throws Exception
     */
    @PostMapping("/insertUserCrop")
    public ResponseResult insertUserCrop(@RequestBody NjkUserCropEntity userCropEntity) throws Exception {
        if (userCropEntity == null)
            return new ResponseResult(ResponseMessage.NULL_REQUEST);
        return userManagerService.insertUserCrop(userCropEntity);
    }

    /**
     * 删除用户作物或者作物下产品信息
     *
     * @return
     * @throws Exception
     */
    @PostMapping("/delUserCrop")
    public ResponseResult delUserCrop(Integer userCropId, Integer productCropId) throws Exception {
        if (userCropId == null && productCropId == null) {
            return new ResponseResult(ResponseMessage.NULL_REQUEST);
        }
        return userManagerService.delUserCrop(userCropId, productCropId);
    }

    /**
     * 用户商品权限的查看
     *
     * @param userId
     * @return
     * @throws Exception
     */
    @PostMapping("/productPriceAuthority")
    public ResponseResult productPriceAuthority(String userId, @RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize) throws Exception {
        if (userId == null || ("").equals(userId))
            return new ResponseResult(ResponseMessage.NULL_REQUEST);
        return userManagerService.productPriceAuthority(userId, pageNum, pageSize);
    }

    /**
     * 删除用户商品权限
     *
     * @param authorityId
     * @return
     * @throws Exception
     */
    @PostMapping("/delProductAuthority")
    public ResponseResult delProductAuthority(String authorityId) throws Exception {
        if (authorityId == null || ("").equals(authorityId))
            return new ResponseResult(ResponseMessage.NULL_REQUEST);
        return userManagerService.delProductAuthority(authorityId);
    }

    /**
     * 用户申请商品权限
     *
     * @param applyVipEntity
     * @return
     * @throws Exception
     */
    @PostMapping("/applyProductAuthority")
    public ResponseResult applyProductAuthority(@RequestBody ApplyVipEntity applyVipEntity) throws Exception {
        if(applyVipEntity == null)
            return new ResponseResult(ResponseMessage.NULL_REQUEST);
        return userManagerService.applyProductAuthority(applyVipEntity);
    }

    /**
     * 查看用户代金券
     *
     * @param userId
     * @return
     * @throws Exception
     */
    @PostMapping("/listUserCoupon")
    public ResponseResult listUserCoupon(String userId, @RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize) throws Exception {
        if (userId == null || ("").equals(userId))
            return new ResponseResult(ResponseMessage.NULL_REQUEST);
        return userManagerService.listUserCoupon(userId, pageNum, pageSize);
    }

    /**
     * 删除用户代金券
     *
     * @param couponId
     * @return
     * @throws Exception
     */
    @PostMapping("/delUserCoupon")
    public ResponseResult delUserCoupon(Integer couponId) throws Exception {
        if (couponId == null)
            return new ResponseResult(ResponseMessage.NULL_REQUEST);
        return userManagerService.delUserCoupon(couponId);
    }

    /**
     * 用户添加代金券
     *
     * @param couponTypeId
     * @return
     * @throws Exception
     */
    @PostMapping("/insertUserCoupon")
    public ResponseResult insertUserCoupon(String userId, Integer couponTypeId) throws Exception {
        if (couponTypeId == null || userId == null || ("").equals(userId))
            return new ResponseResult(ResponseMessage.NULL_REQUEST);
        return userManagerService.insertUserCoupon(userId, couponTypeId);
    }

    /**
     * 用户电话日状态修改
     *
     * @param userId
     * @return
     * @throws Exception
     */
    @PostMapping("/updatePhoneStatus")
    public ResponseResult updatePhoneStatus(String userId, String phoneResult) throws Exception {
        if (userId == null || ("").equals(userId) || phoneResult == null || ("").equals(phoneResult))
            return new ResponseResult(ResponseMessage.NULL_REQUEST);
        return userManagerService.updatePhoneStatus(userId, phoneResult);
    }

    /**
     * 用户信息导出表格
     *
     * @return
     * @throws Exception
     */
    @GetMapping("/userOutExcel")
    public ResponseResult userOutExcel(HttpServletResponse response) throws Exception {
        return userManagerService.userOutExcel(response);
    }

    /**
     * 用户积分详情
     *
     * @param userId
     * @return
     * @throws Exception
     */
    @PostMapping("/listUserIntegral")
    public ResponseResult listUserIntegral(String userId, @RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize) throws Exception {
        if (userId == null || ("").equals(userId))
            return new ResponseResult(ResponseMessage.NULL_REQUEST);
        return userManagerService.listUserIntegral(userId, pageNum, pageSize);
    }

    /**
     * 申请积分变动
     *
     * @param userId
     * @param news
     * @return
     * @throws Exception
     */
    @PostMapping("/updateUserIntegral")
    public ResponseResult updateUserIntegral(String userId, Integer communityType, String news, Integer integralNum) throws Exception {
        if (userId == null || ("").equals(userId) || integralNum == null)
            return new ResponseResult(ResponseMessage.NULL_REQUEST);
        return userManagerService.updateUserIntegral(userId, communityType, news, integralNum);
    }

    /**
     *每日运营数据
     * @param status 0 周 1 月 2 年
     * @return
     */
    @PostMapping("/dailyDatabase")
    public ResponseResult dailyDatabase(Integer status) throws Exception {
        return userManagerService.dailyDatabase(status);
    }

    /**
     * 每日运营导出excel
     * @param startTime
     * @param endTime
     * @return
     */
    @RequestMapping("/outputExcleOperator")
    public ResponseResult outputExcleOperator(HttpServletResponse response, String startTime, String endTime) throws Exception {
        return userManagerService.outputExcleOperator(response, startTime, endTime);
    }
}
